package com.djf.Scrm.dataRelay.service;

import cn.hutool.core.codec.Base64;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.djf.Scrm.dataRelay.dao.UserInfoDao;
import com.djf.Scrm.dataRelay.dao.UserInfoRq;
import com.djf.Scrm.dataRelay.dao.WxConfigDao;
import com.djf.Scrm.dataRelay.mapper.WxServiceMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class WxService {
    @Autowired
    private WxConfigDao wxConfigDao;
    @Autowired
    private WxServiceMapper wxServiceMapper;

    public UserInfoDao getWechat(UserInfoRq userInfoRq) throws Exception {
        UserInfoDao userInfo = wxServiceMapper.getUserInfo(userInfoRq);

        String wxUrl = wxConfigDao.getUrl();
        String url = wxUrl+"?appid={0}&secret={1}&js_code={2}&grant_type=authorization_code";
        String appid = wxConfigDao.getAppid(); //"wx8181947b99c593e9";
        String secret = wxConfigDao.getSecret(); //"8998487cbf0b7d18a895a27869d06667";
        String replaceUrl = url.replace("{0}", appid).replace("{1}", secret).replace("{2}", userInfoRq.getCode());
        String res = HttpUtil.get(replaceUrl);//后面有代码的，莫急
        userInfo.setWxAppId(res);

        return userInfo;
    }
}
